package com.ias.agdis_r.action.bill;

import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.util.Log;

import com.ias.agdis_r.R;
import com.ias.agdis_r.action.task.TaskReceiveAction;
import com.ias.agdis_r.bean.document.Document;
import com.ias.agdis_r.bean.document.DocumentResult;
import com.ias.agdis_r.db.BoxDBHelper;
import com.ias.agdis_r.impl.bill.BillResponseImpl;
import com.ias.agdis_r.manager.ActionManager;
import com.ias.agdis_r.manager.DataBaseManager;
import com.ias.agdis_r.manager.DataManager;
import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.terminalmanager.restful.base.endpoint.response.BaseResponse;
import com.ias.agdis_r.terminalmanager.restful.bill.endpoint.response.BillDetailResponse;
import com.ias.agdis_r.terminalmanager.restful.bill.endpoint.response.BillGeneralResponse;
import com.ias.agdis_r.terminalmanager.restful.bill.endpoint.response.BillPrintResponse;
import com.ias.agdis_r.terminalmanager.restful.bill.vo.BillDetailResponseVO;
import com.ias.agdis_r.terminalmanager.restful.bill.vo.BillGeneralResponseVO;
import com.ias.agdis_r.util.S;
import com.ias.agdis_r.util.TaskUtil;

public class BillResponseAction implements BillResponseImpl {
    private Context context = DataBaseManager.getInstance().getContext();

    @Override
    public void receiveBLRQ(BaseResponse response) {
        // TODO Auto-generated method stub
        BillGeneralResponse generalResponse = (BillGeneralResponse) response;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(generalResponse.getHeader().getCurrentTimestamp());
        BillGeneralResponseVO generalResponseVO = generalResponse.getBillGeneraResponse();
        if (null == generalResponseVO) {
            return;
        }
        String fke = generalResponseVO.getFkey();
        String fln = generalResponseVO.getFln();
        if (S.isNotEmpty(fke)) {
            DocumentResult newdocList = generalResponseVO.getBillResult();
            if (newdocList == null) {
                Log.i("BillResponseAction", "receiveBLRQ DocumentResult is null!");
                return;
            }
            DocumentResult oldDocList = DataManager.getInstance().getDocList(fke);
            List<String> tnbList = TaskUtil.getTnbByFke(fke);
            // 如果没有单据摘要信息，刷新摘要信息
            if (oldDocList == null || oldDocList.getDocumentList().size() == 0) {
                DataManager.getInstance().putDocList(fke, newdocList);
                if (tnbList.size() > 0) {
                    saveData(fke, context.getString(R.string.BRA_1) + fln + context.getString(R.string.BRA_2), MessageModel.BLRQ, time);
                    // 播放提示音
                    TaskReceiveAction.getInstance().playEffect(MessageModel.BLRQ);
                    EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK, context.getString(R.string.findBill), false);
                }
            } else {
                List<Document> newDocList = newdocList.getDocumentList();
                // 比较获取单据的版本号
                for (int i = 0; i < newDocList.size(); i++) {
                    Document newDocument = newDocList.get(i);
                    if (S.isNotEmpty(newDocument.getBTP())) {
                        List<Document> olddocList = oldDocList.getDocumentList();
                        for (int j = 0; j < olddocList.size(); j++) {
                            if (olddocList.get(j).getBTP().equals(newDocument.getBTP())) {
                                // 如果新版本的版本号大于旧版本的版本号刷新页面
                                if (Integer.valueOf(newDocument.getBNO()) > Integer.valueOf(olddocList.get(j).getBNO())) {
                                    olddocList.set(j, newDocument);
                                    if (tnbList.size() > 0) {
                                        saveData(fke, context.getString(R.string.BRA_3) + fln + context.getString(R.string.BRA_4) + newDocument.getBNM() + context.getString(R.string.BRA_5), MessageModel.BLRQ, time);
                                        // 播放提示音
                                        TaskReceiveAction.getInstance().playEffect(MessageModel.BLRQ);
                                        EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK, context.getString(R.string.updateBill), false);
                                    }
                                    newDocList.remove(newDocument);
                                    i--;
                                } else {
                                    newDocList.remove(newDocument);
                                    i--;
                                }
                            }
                        }
                    }
                }
                if (newdocList.getDocumentList().size() > 0) {
                    DataManager.getInstance().getDocList(fke).getDocumentList().addAll(newdocList.getDocumentList());
                    if (tnbList.size() > 0) {
                        saveData(fke, context.getString(R.string.BRA_1) + fln + context.getString(R.string.BRA_2), MessageModel.BLRQ, time);
                        // 播放提示音
                        TaskReceiveAction.getInstance().playEffect(MessageModel.BLRQ);
                        EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK, context.getString(R.string.findBill), false);
                    }
                }
            }
        }
    }

    private void saveData(String fke, String msg, String serviceId, String time) {
        BoxDBHelper dbhelper = new BoxDBHelper(DataBaseManager.getInstance()
                .getContext());
        dbhelper.setFlightData(fke, msg, serviceId, time);
        dbhelper.closeDB();
        DataManager.getInstance().addUnCheckNumber();
        // 刷新任务列表UI或者刷新任务详情UI
        ActionManager.getInstance().refreshBox(MessageModel.REFRESH_BOXFLIGHT);
        ActionManager.getInstance().updateUI(MessageModel.REFRESH_BOXFLIGHT);
    }

    @Override
    public void receiveBLDT(BaseResponse response) {
        // TODO Auto-generated method stub
        BillDetailResponse billdetailResponse = (BillDetailResponse) response;
        BillDetailResponseVO billdetailResponseVO = billdetailResponse
                .getBillDetailResponse();
        String fkey = billdetailResponseVO.getFkey();
        if (S.isNotEmpty(fkey)) {
            String doctxt = billdetailResponseVO.getData();
            String btp = billdetailResponseVO.getBtp();
            DocumentResult docResult = DataManager.getInstance().getDocList(
                    fkey);
            for (Document doc : docResult.getDocumentList()) {
                if (btp.equals(doc.getBTP())) {
                    doc.setTXT(doctxt);
                    // Body.getInstance().updateUI(fke,ParameterModel.UI_UPDATE_DOCDETAIL);
                    break;
                }
            }

        }
    }

    @Override
    public void receiveBLPT(BaseResponse response) {

        BillPrintResponse billPrintResponse = (BillPrintResponse) response;

        String fke = billPrintResponse.getBillPrintResponseVO().getFkey();
        String btp = billPrintResponse.getBillPrintResponseVO().getBtp();

        DocumentResult docResult = DataManager.getInstance().getDocList(fke);
        for (Document doc : docResult.getDocumentList()) {
            if (btp.equals(doc.getBTP())) {
                doc.setSTS("S");
                EffectManager.getInstance().ttsPlay(
                        EffectManager.TTS_TYPE_TASK, context.getString(R.string.printSucceed), false);
                // Body.getInstance().updateUI(fke,
                // ParameterModel.UI_UPDATE_DOCSUCCESS);
                break;
            }
        }
    }

}
